#include "unsortedVector.h"

using namespace std;


//Constructor
unsortedVector::unsortedVector()
{
	vectorNodes = new vector<Node*>(0);
}


//Adds a node to the vector
void unsortedVector::addNode(Node* node)
{
	vectorNodes->push_back(node);
}


//Returns true if the vector is empty
bool unsortedVector::empty()
{
	return vectorNodes->empty();
}


//Removes a node from the vector
void unsortedVector::remove(std::vector<Node*>::iterator it)
{
	vectorNodes->erase(it);
}


//Allocates memory for the vector
void unsortedVector::allocate(int width, int height)
{
	vectorNodes->reserve(width*height);
}


//Frees every node in the vector
void unsortedVector::freeNodes()
{
	for(vector<Node*>::iterator it = vectorNodes->begin(); it != vectorNodes->end(); ++it)
	{
		delete *it;
	}

	delete vectorNodes;
}


//Returns the node with lowest score from the vector
Node* unsortedVector::selectNodeLowestScore()
{
	//Initialise the lowest node with the first node in the vector
	Node* lowestNode = vectorNodes->front();

	//Initialise the iterator that will point to the lowest node
	vector<Node*>::iterator itLowestNode = vectorNodes->begin();

	//Scan every node in the vector and save in lowestNode the node with lowest score
	for (vector<Node*>::iterator it = vectorNodes->begin(); it != vectorNodes->end(); ++it)
	{
		if( (*it)->getF() < lowestNode->getF())
		{
			lowestNode = *it;
			itLowestNode = it;
		}
	}

	//Remove the lowest node from the vector
	vectorNodes->erase(itLowestNode);
	
	return lowestNode;
}


//Look for a node in the vector
pair<vector<Node*>::iterator, Node*> unsortedVector::findNode(int x, int y)
{
	pair<vector<Node*>::iterator, Node*> p;
	vector<Node*>::iterator it;
	for(it = vectorNodes->begin(); it != vectorNodes->end(); ++it)
	{
		if( (*it)->getX() == x && (*it)->getY() == y)
		{
			p.first = it;
			p.second = *it;
			return p;
		}
	}

	p.first = it;
	p.second = 0;
	return p;
}


//Modify the attributes of the node
void unsortedVector::modifyNode(std::vector<Node*>::iterator it)
{}